<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.keyword" placeholder="商铺名称"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="queryListData">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="depts" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="name" label="商铺名称" >
			</el-table-column>
			<el-table-column prop="employee.username" label="商铺管理员" >
			</el-table-column>
			<el-table-column prop="logo" label="商铺标志" >
				<template scope="scope">
					<el-image
							style="width: 100px; height: 100px"
							:src="fastDfsUrl + scope.row.logo">
					</el-image>
				</template>
			</el-table-column>
			<el-table-column prop="state" label="商铺状态" >
				<template scope="scope">
					<span style="color: #11b95c" v-if="scope.row.state===1">审核通过</span>
					<span style="color: #1d8ce0" v-else-if="scope.row.state===0">待审核</span>
					<span style="color: red" v-else-if="scope.row.state===-1">驳回待修改</span>
				</template>
			</el-table-column>
			<el-table-column label="操作" >
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">审核</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>



		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="pageSize" :total="total" style="float:right;">
			</el-pagination>
		</el-col>

		<!--操作界面-->
		<el-dialog :title="title" :visible.sync="editFormVisible" :close-on-click-modal="false">
			<el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
				<el-form-item label="商铺名称" prop="name">
					<el-input v-model="addForm.name" ></el-input>
				</el-form-item>
				<el-form-item label="商铺电话">
					<el-input v-model="addForm.tel" ></el-input>
				</el-form-item>
				<el-form-item label="商铺地址">
					<el-input v-model="addForm.address" ></el-input>
				</el-form-item>
				<el-form-item label="状态">
					<el-radio-group v-model="addForm.state">
						<el-radio class="radio" :label="-1">驳回</el-radio>
						<el-radio class="radio" :label="1">审核通过</el-radio>
					</el-radio-group>
				</el-form-item>
				<el-form-item prop="logo" label="店铺Logo">
					<el-upload
							class="upload-demo"
							action="/api/dfs/upload"
							:on-preview="handlePreview"
							:on-remove="handleRemove"
							:on-success="handleSuccess"
							:file-list="fileList"
							list-type="picture">
						<el-button size="small" type="primary" >点击上传</el-button>
						<div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
					</el-upload>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	export default {
		data() {
			return {
			    fastDfsUrl:"http://115.159.217.249:8888",
			    shop:{
			        logo:'',
				},
                fileList:[],
                depts:[],
                url:'',
				deptId:"",
				filters: {
					keyword: ''
				},
                empfilters:{
                    keyname:''
				},
                title:'新增',// 默认模态框标题为新增
                etitle:'新增',// 默认模态框标题为新增
				total: 0,// 当前页
				page: 1,
                pageSize:5,// 每页条数
                currentPage:1,// 默认第一页
				listLoading: false,
				sels: [],//列表选中列
                editFormVisible:false,
				editLoading: false,
				editFormRules: {
					name: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					]
				},
				//编辑界面数据
                addForm:{//双向绑定的对象
                    id:null,
					name:'',
					tel:'',
					registerTime:'',
					state:'',
					address:'',
					logo:'',
					admin_id:'',
                    employee:[],
                },
				addLoading: false,
				addFormRules: {
					name: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					]
				},

			}
		},
		methods: {
		    /*分页*/
			handleCurrentChange(page) {

                this.currentPage=page;
				this.loadListData();
			},
			//获取用户列表
			loadListData() {
				let para = {
                    currentPage: this.currentPage,
                    pageSize:5,
					keyword: this.filters.keyword,
				}
				this.$http.post("/shop",para).then(res=>{
                    /*res是返回的数据*/
                    this.depts=res.data.bags;
				    this.total=res.data.total;
				})
			},
			/*高级查询*/
            queryListData(){
                currentPage: this.currentPage=1;
                this.loadListData();
			},
			//删除
			handleDel(i, row) {
                this.$confirm('亲，确认删除数据?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(()=>{
                let id = row.id;
                this.$http.delete("/shop/"+id).then(res=>{
                    if(res.data.success){
                        this.$message({
                            type:'success',
                            message:res.data.message
						});
					}else {
                        this.$message({
                            type: 'error',
                            message: res.data.message
                        });
					}
					// 刷新列表
					this.loadListData();
				})
                }).catch(()=>{
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                })

			},
			//显示编辑界面
			handleEdit(i, row) {
			    /*设置表标题*/
            	this.title='编辑';
            	/*显示模态框*/
				this.editFormVisible = true;
				/*设置回显*/
                let para=Object.assign(this.addForm, row);
			},
			//显示新增界面
			handleAdd() {
                /*设置表标题*/
                this.title='编辑';
                /*清空列表*/
                this.addForm={//双向绑定的对象
                    	id:null,
                        name:'',
                    	tel:'',
                        state:0,
                    	registerTime:'',
                    	address:'',
                    	logo:'',
                        employee:null
                };
                /*显示模态框*/
                this.editFormVisible = true;
			},
			//新增编辑
			addSubmit(){
			    /*检验表单规则*/
			    this.$refs.addForm.validate((valid)=>{
			        if(valid){// 符合规则
			            this.$confirm('确认提交吗？','提示',{}).then(()=>{
			                /*加载效果开启*/
			                this.addLoading = true;
			                /*拷贝对象*/
                            let para=Object.assign({}, this.addForm);
                            this.$http.put("/shop/update",para).then(res=>{
                                if(res.data.success){
                                    this.$message({
                                        type: 'success',
                                        message: res.data.message
                                    });
                                    //刷新数据
                                    this.loadListData();
                                }else {
                                    this.$message({
                                        type: 'error',
                                        message: res.data.message
                                    });
                                }
                            })
                            //关闭加载效果
                            this.addLoading=false;
                            //关闭模态框
                            this.editFormVisible=false;
						})
					}
				})
			},
			selsChange(sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove() {
			    /*获取选中的id*/
                var ids = this.sels.map(item => item.id);
				/*删除警告*/
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
				    /*打开警告框*/
					this.listLoading = true;
					this.$http.patch("/shop",ids).then(res=>{
                        this.listLoading = false;
                        if(res.data.success){
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                        }else{
                            this.$message({
                                message: res.data.msg,
                                type: 'error'
                            });
                        }
					})
						this.loadListData();
				}).catch(() => {

				})
			},
			/*图片上传功能实现*/
            handleSuccess(response, file, fileList){
                console.log("===========")
                console.log(response);
                console.log(file);
                console.log(fileList);
                this.shop.logo = response.resultObj;
            },
            handleRemove(file, fileList) {
                var filePath =file.response.resultObj;
                this.$http.delete("/dfs/?path="+filePath)
                    .then(res=>{
                        if(res.data.success){
                            this.$message({
                                message: '删除成功!',
                                type: 'success'
                            });
                        }else{
                            this.$message({
                                message: '删除失败!',
                                type: 'error'
                            });
                        }
                    })
            },
            handlePreview(file) {
                console.log(file);
            },
		},
		mounted() {
			this.loadListData();
		}
	}

</script>

<style scoped>

</style>